package mvc.model.dao.impl;

import mvc.common.BaseDao;
import mvc.model.dao.ProductDao;
import mvc.model.pojos.PictureBean;
import mvc.model.pojos.ProductBean;
import mvc.model.pojos.RomBean;
import mvc.model.vo.ColorVO;
import mvc.model.vo.HeadProductVO;
import mvc.model.vo.ProListBean;

import java.util.List;

public class ProductDaoImpl implements ProductDao {
    private static BaseDao BASE_DAO = new BaseDao();
    @Override
    public List<ProductBean> queryByName(String pname) {
        String sql = "SELECT * FROM product WHERE pname = ?;";
        return BASE_DAO.query(sql, ProductBean.class, pname);
    }

    @Override
    public ProductBean queryById(int pid) {
        String sql = "SELECT * FROM product WHERE pid = ?";
        List<ProductBean> query = BASE_DAO.query(sql, ProductBean.class, pid);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public List<HeadProductVO> findSome() {
        String sql = "SELECT p.pid , `pname`, price,`path` FROM product as p,p_p,ppicture as pp where p.pid = p_p.pid and p_p.pnumber = pp.pnumber group by p.pname order by p.lasttime desc limit 8;";
        return BASE_DAO.query(sql, HeadProductVO.class);
    }

    @Override
    public List<ColorVO> getColor(String pname) {
        String sql = "select c.cid,c.color,p.pid,pp.path from product p,color c, p_info pi, p_p, ppicture pp where pname = ? and p.number = pi.number and pi.cid = c.cid and p.pid = p_p.pid and p_p.pnumber = pp.pnumber and p_p.main = 1 and p_p.flag = 1;";
        return BASE_DAO.query(sql, ColorVO.class, pname);
    }

    @Override
    public List<RomBean> getRom(String pname) {
        String sql = "select r.rid,p.pid,r.rsize,r.msize from product p, p_info pi,rom r where pname = ? and p.number = pi.number and pi.rid = r.rid;";
        return BASE_DAO.query(sql, RomBean.class, pname);
    }

    @Override
    public List<PictureBean> getPicture(int pid) {
        //language=MySQL
        String sql = "SELECT pp.pnumber,pp.path FROM ppicture pp,p_p p where p.pnumber=pp.pnumber and p.pid = ?;";
        return BASE_DAO.query(sql, PictureBean.class, pid);
    }

    @Override
    public List<PictureBean> queryPic(int cid, String pname) {
        String sql = "select pnumber,path from ppicture pic join " +
                "p_p pp on pic.pnumber = pp.pnumber join " +
                "product p on pp.pid = p.pid join " +
                "p_info pin on pin.number = p.pname where cid = ? and pname = ? ";
        return BASE_DAO.query(sql, PictureBean.class,cid,pname);
    }

    @Override
    public List<ProListBean> queryList() {
        String sql = "select pid,pname,price,path from pro pro join p_p pp on pro.pid = pp.pid " +
                "join ppicture pic on pp.pnumber = pic.pnumber group by pname";
        return BASE_DAO.query(sql,ProListBean.class);
    }

    @Override
    public List<ProListBean> queryProList(String index) {
        StringBuffer sql = new StringBuffer("select pid,pname,price,path from pro pro join p_p pp on pro.pid = pp.pid " +
                "join ppicture pic on pp.pnumber = pic.pnumber group by pname where 1=1");
        if (index.equals("2")){
            sql.append("flag = 1");
        }else if (index.equals("3")){
            sql.append("flag = 2");
        } else if (index.equals("4")) {
            //按价格从大到小排列
            sql.append("order by price desc");
        } else if (index.equals("5")) {
            //按价格从大到小排列
            sql.append("order by price");
        }
        return BASE_DAO.query(sql.toString(),ProListBean.class);
    }
}
